package com.moro.lisp.tools;


public class JLispQueue {
	private Object[] datas;
	
	public static int MINCAPACITY = 10;
	
	private int capacity;
	
	private int size;
	
	private int headIndex; //队列头部索引
	
	private int tailIndex; //队列尾部索引
	
	public JLispQueue() {
		this.datas = new Object[JLispQueue.MINCAPACITY];
		this.capacity = this.datas.length;
		this.size = 0;
		this.headIndex = 0;
		this.tailIndex = -1;
	}
	
	public void add(Object data) { //由于null被用作错误检测，因此不允许添加null值
		if(null == data) {
			return;
		}
		this.size = this.size + 1 > this.capacity ?
					this.capacity :
					this.size + 1;
		this.tailIndex = (this.tailIndex + 1) % this.capacity;
		if(1 != this.size && this.headIndex == this.tailIndex) {
			this.headIndex = (this.headIndex + 1) % this.capacity;
		}
		this.datas[this.tailIndex] = data;
	}
	
	public Object remove() {
		if(0 == this.size) {
			return null;
		}
		this.size = this.size - 1 < 0 ?
					0 :
					this.size - 1;
		Object data = this.datas[this.headIndex];
		this.datas[this.headIndex] = null;
		this.headIndex = (this.headIndex + 1) % this.capacity;
		return data;
	}
	
	public void addCapacity(int addCapacity) { //扩容
//		int tempCapacity = this.capacity + addCapacity;
//		if(JLispQueue.MINCAPACITY > tempCapacity) {
//			return;
//		}
//		//this.capacity = tempCapacity;
//		JLispData[] tempDatas = new JLispData[this.capacity];
//		for(int i = 0;i < this.size;i++) {
//			
//		}
		
	}
	
	public void subCapacity(int subCapacity) { //减容
//		int tempCapacity = this.capacity - subCapacity;
//		if(JLispQueue.MINCAPACITY > tempCapacity) {
//			return;
//		}
//		this.size = tempCapacity;
	}
	
	public Object getHead() {
		return 0 == this.size ?
				null :
				this.datas[this.headIndex];
	}
	
	public Object getTail() {
		return 0 == this.size ?
				null :
				this.datas[this.tailIndex];
	}
	
	public Object get(int index) {
		return index < 0 || index >= this.size ?
				null :
				this.datas[queInToArrIn(index)];
	}
	
	public Object remove(int index) {
		return null;
	}
	
	private int queInToArrIn(int index) { //队列索引转换成数组索引
		return index < 0 || index >= this.size ?
				-1 :
				(index + this.headIndex) % this.capacity;
	}
	
	private int arrInToQueIn(int index) { //数组索引转换成队列索引
		return index < 0 || index >= this.capacity ?
				-1 :
				index < this.headIndex ?
				index - this.headIndex + this.capacity :
				index - this.headIndex;
	}
//	public static void main(String[] args) {
//		JLispQueue queue = new JLispQueue();
//		SymbolPool symbolPool = new SymbolPool();
//		int num = 0;
//		while(true) {
//			Random rand = new Random();
//			int p = rand.nextInt(5);
//			if(p >= 2) {
//				System.out.println("添加");
//				queue.add(new JLispData(symbolPool.getSymbolByString("" + num++), JLispType.ATOM));
//			}else {
//				System.out.println("移除");
//				queue.remove();
//			}
//			for(int i = 0;i < queue.size;i++) {
//				System.out.print(((Atom)(queue.get(i).data)).name + "\t");
//			}
//			System.out.println();
//			try {
//				Thread.sleep(1000);
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//	}
}
